/*
 * Copyright (c) 2017. 深圳一七科技. All rights reserved.
 */

package com.yiqi.liebang.feature.order.presenter;

import android.text.TextUtils;

import com.suozhang.framework.utils.T;
import com.yiqi.liebang.entity.bo.AnswerBo;
import com.yiqi.liebang.entity.bo.CommentBo;
import com.yiqi.liebang.entity.bo.OrderQuestionInfofBo;
import com.yiqi.liebang.entity.bo.QuestionDetailBo;
import com.yiqi.liebang.feature.order.contract.QuestionInfoContract;
import com.yiqi.liebang.feature.order.dependencies.questionInfo.DaggerQuestionInfoPresenterComponent;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * LIJUWEN
 *
 * @email yiyayiyayaoljw@gmail.com
 * @date 2018/7/11 9:31
 */
public class QuestionInfoPresenter implements QuestionInfoContract.Presenter {

    private QuestionInfoContract.View mView;

    @Inject

    QuestionInfoContract.Model mModel;


    public QuestionInfoPresenter(QuestionInfoContract.View mView) {
        this.mView = mView;
        DaggerQuestionInfoPresenterComponent.create().inject(this);
    }

    @Override
    public void start() {

    }


    @Override
    public void getOrderQuestionInfo(String id,int type) {
        mModel.getOrderQuestionInfo(id,type)
                .compose(mView.bindToLife())
                .subscribe(new Observer<OrderQuestionInfofBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(OrderQuestionInfofBo infofBo) {
                        mView.dismissLoading();
                        mView.showOrderQuestionInfoSuccess(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderQuestionInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void refusedQuestion(String id) {
        mModel.refusedQuestion(id)
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String infofBo) {
                        mView.dismissLoading();
                        mView.showRefusedQuestion(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderQuestionInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void toAnswer(String id, String answerContent, int type) {
        if (TextUtils.isEmpty(answerContent)){
            T.showShort("请输入回答内容！");
            return;
        }

        mModel.toAnswer(new AnswerBo(id,answerContent,type))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String infofBo) {
                        mView.dismissLoading();
                        mView.showAnswerSuccess(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderQuestionInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void toComment(String id, float score) {
        if (score<=0){
            T.showShort("请选择评分！");
            return;
        }

        mModel.toAnswer(new CommentBo(score,id))
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String infofBo) {
                        mView.dismissLoading();
                        mView.showCommentSuccess(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderQuestionInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getOrderQuestionDetail(String id) {
        if (TextUtils.isEmpty(id)) return;
        CommentBo commentBo=new CommentBo(id);
        mModel.getOrderQuestionDetail(commentBo)
                .compose(mView.bindToLife())
                .subscribe(new Observer<QuestionDetailBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(QuestionDetailBo infofBo) {
                        mView.dismissLoading();
                        mView.showOrderQuestionDetailSuccess(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderQuestionInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void toCommentInfo(Integer chargeState, String questionId, float score) {
        if (TextUtils.isEmpty(questionId)) return;
        CommentBo commentBo=new CommentBo();
        commentBo.setQuestionId(questionId);
        commentBo.setScore(score);
        commentBo.setChargeState(chargeState);
        mModel.toCommentQuestion(commentBo)
                .compose(mView.bindToLife())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mView.showLoading(d);
                    }

                    @Override
                    public void onNext(String infofBo) {
                        mView.dismissLoading();
                        mView.showCommentDetailSuccess(infofBo);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoading();
                        mView.showOrderQuestionInfoError(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
